using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Awesome.Extensions.Reflection
{
    public static class AttributeEx
    {
        /// <summary>
        /// Returns attribute of type TAttribute or null, if member is not attributed.
        /// </summary>
        public static TAttribute GetAttribute<TAttribute>(this ICustomAttributeProvider provider, bool inherit = false)
            where TAttribute : Attribute
        {
            return (TAttribute) provider.GetCustomAttributes(typeof (TAttribute), inherit).FirstOrDefault();
        }


        /// <summary>
        /// Returns all attributes of type TAttribute.
        /// </summary>
        public static IEnumerable<TAttribute> GetAllAttributes<TAttribute>(this ICustomAttributeProvider provider,
                                                                          bool inherit = false)
            where TAttribute : Attribute
        {
            return provider.GetCustomAttributes(typeof (TAttribute), inherit).Cast<TAttribute>();
        }

    }
}